<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Yingyonghui
 *
 * @author dragon.hu add @2016.2.23
 */
require_once 'BaseChannel.php';

//应用汇
class Yingyonghui implements BaseChannel 
{
	
	function __construct() 
	{
		//echo "construct";
	}

	//put your code here
	function android($data) 
	{
		$paymentKey="REIyQ0MxQ0ZCRUMyRUM3MTZBM0E0MkYwRDYxRDM4RTJFMjIxQjc5OU1URXpNRGc0TXpBd016ZzFNamsyTXpZMk1URXJNakExTlRNM05qQTROVFE1TXpBM05EVXdPRFkzT0RZeE9UTTNORGt6TmpZek5ESTJOemd4"; //支付秘钥
		$ret = $this->paymentCallBack($paymentKey, ANDROID);
		echo $ret;
	}

	function ios($data) 
	{
		$paymentKey=""; //
		$ret = $this->paymentCallBack($paymentKey, IOS);
		echo $ret;
	}

	function paymentCallBack($paymentKey, $platform)
	{
		require_once ('include/yingyonghui/IappDecrypt.php');
		$orderLog = new ChargeOrder();
		$orderLog->log("yingyonghui", $_REQUEST);
		
		$sign = $_REQUEST['sign'];
		$sign = trim($sign);  //此处最后有空格，收到的数据做下 trim 处理
		$transdata = $_REQUEST['transdata'];
		$transdata = str_replace('\\', "", trim($transdata));  //处理'\'
		
		//var_dump($sign);
		//var_dump(trim($sign));
		//var_dump($transdata);
		//var_dump(str_replace('\\', "", trim($transdata)));

		$tools = new IappDecrypt();
		$result = $tools->validsign($transdata,$sign,$paymentKey);
		
		$data = json_decode($transdata, true);
		$orderLog->log("yingyonghui", $data);
		//var_dump($result);
		//var_dump($data);
		//exit();
		
		if($result == 0) 
		{ //验证通过
			 $status = STATUS_SUCCESS;
			 if ($data['result'] != 0)
			 {
			 	$status = STATUS_FAILD;
			 }
			//进行发货
			if(false == $orderLog->recordOrder($status, $data['money']/100, $data['exorderno'], "yingyonghui", $platform,$data['transid']))
			{
				$ret="FAILED";
				$orderLog->log("yingyonghui",array("order"=>$data['exorderno'],"money"=>$data['money'],"serverresult"=>"faild"));
			}else 
			{
				//支付成功
				$ret="SUCCESS";
				$orderLog->log("yingyonghui",array("order"=>$data['exorderno'],"money"=>$data['money'],"serverresult"=>"success"));
			} 
		} 
		else 
		{
			$ret = "FAILED";
			$orderLog->log("yingyonghui","validsign error");
		}
		return $ret;
	}
	
}